﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoMapper;
using MongoDB.Driver;
using MongoManager.Tests.Entities;
using MongoManager.Tests.Converters;

namespace MongoManager.Tests
{

    [TestClass]
    public class MongoManagerTests
    {
        private TestContext testContextInstance;
        private const string DATABASE_NAME = "MongoManagerTestDB";
        
        public MongoManagerTests()
        {
            Mapper.CreateMap<Document, User>().ConvertUsing<DocumentToUserConverter>();
            Mapper.CreateMap<User, Document>().ConvertUsing<UserToDocumentConverter>();
        }

        

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestCleanup]
        public void MyTestCleanup()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            List<User> users = manager.FindAll();

            foreach (User user in users)
            {
                manager.Delete(user);
            }
        }

        [TestMethod]
        public void TestFind()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            User newUser = new User();

            newUser.UserName = "MyTest1";
            newUser.Password = "testpassword";
            newUser.EmailAddress = "mytest@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            // First, save it.
            manager.Save(ref newUser);

            // Now, try to find it.
            List<User> users = manager.Find(newUser);

            Assert.IsTrue(users.Count > 0);

        }

        [TestMethod]
        public void TestDelete()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            User newUser = new User();

            newUser.UserName = "MyTest1";
            newUser.Password = "testpassword";
            newUser.EmailAddress = "mytest@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            // First, save it.
            manager.Save(ref newUser);

            // Now, try to find it.
            List<User> users = manager.Find(newUser);

            // Delete it.
            manager.Delete(users[0]);

            // And, try to find it again.
            users = manager.Find(newUser);

            Assert.IsTrue(users.Count == 0);
        }

        [TestMethod]
        public void TestInsert()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            User newUser = new User();

            newUser.UserName = "MyTest1";
            newUser.Password = "testpassword";
            newUser.EmailAddress = "mytest@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            manager.Save(ref newUser);

        }

        [TestMethod]
        public void TestUpdate()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            User newUser = new User();

            newUser.UserName = "MyTest1";
            newUser.Password = "testpassword";
            newUser.EmailAddress = "mytest@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            // First, save it.
            manager.Save(ref newUser);

            // Now, try to find it.
            List<User> users = manager.Find(newUser);

            if (users.Count > 0)
                newUser = users[0];
            else
                Assert.IsTrue(false);

            // Change the email address.
            newUser.EmailAddress = "newemailaddress@test.com";

            // Save it.
            manager.Save(ref newUser);

            // Get, and check it to see if it saved.
            users = manager.Find(newUser);

            Assert.IsTrue(users[0].EmailAddress == "newemailaddress@test.com");


        }

        [TestMethod]
        public void TestFindAll()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            User newUser = new User();

            newUser.UserName = "MyTest1";
            newUser.Password = "testpassword";
            newUser.EmailAddress = "mytest@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            // First, save it.
            manager.Save(ref newUser);

            newUser = new User();
            newUser.UserName = "MyTest2";
            newUser.Password = "testpassword2";
            newUser.EmailAddress = "mytest2@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            // Save this one
            manager.Save(ref newUser);

            // Now, get them all
            List<User> users = manager.FindAll();

            Assert.IsTrue(users.Count == 2);

        }

        [TestMethod]
        public void TestLoad()
        {
            MongoManager<User> manager = new MongoManager<User>(DATABASE_NAME, "Users");

            User newUser = new User();

            newUser.UserName = "MyTest1";
            newUser.Password = "testpassword";
            newUser.EmailAddress = "mytest@test.com";
            newUser.RegistrationDate = DateTime.Now;
            newUser.LastLoginDate = DateTime.Now;

            // First, save it.
            manager.Save(ref newUser);

            Assert.IsTrue(!string.IsNullOrEmpty(newUser.ID));

        }

    }
}
